package com.xinxin.facelinker.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.easemob.chatuidemo.R;
import com.lidroid.xutils.HttpUtils;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.NearUserMotionAdapter;
import com.xinxin.facelinker.domain.ShowNearUserMotion;
import com.xinxin.facelinker.ui.button.views.ButtonFloat;
import com.xinxin.facelinker.ui.menu.ContextMenuDialogFragment;
import com.xinxin.facelinker.ui.menu.MenuObject;
import com.xinxin.facelinker.ui.menu.MenuParams;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemClickListener;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

public class ShowNearUserMotionActivity extends FragmentActivity implements OnMenuItemClickListener,
        OnMenuItemLongClickListener {

//    private ListView lvShowNearUserMotion;
//    private ArrayAdapter adapter;
//    private LocationManager locationManager;
//    private Location location;
//    private StringBuilder longitude;
//    private StringBuilder latitude;
//    private String my_account_num;
//    private HttpUtils httpUtils = new HttpUtils();
//    private List<ShowNearUserMotion> list = new ArrayList<ShowNearUserMotion>();
    private View btnBack;
    private ButtonFloat btnMenu;
    private FragmentManager fragmentManager = getSupportFragmentManager();
    private DialogFragment mMenuDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_near_user_motion);

//        lvShowNearUserMotion = (ListView) findViewById(R.id.lvShowNearUserMotion);
        btnBack=findViewById(R.id.btnBack);
//        my_account_num = getIntent().getStringExtra(Config.KEY_MY_ACCOUNT_NUM);
//        adapter = new ArrayAdapter<NearUserMotionAdapter>(ShowNearUserMotionActivity.this, android.R.layout.simple_list_item_1);
//        lvShowNearUserMotion.setAdapter(adapter);
//        longitude = new StringBuilder();
//        latitude = new StringBuilder();
//
        btnMenu = (ButtonFloat) findViewById(R.id.btnMenu);
//
//        initMenuFragment();
//
        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenuDialogFragment.onAttach(ShowNearUserMotionActivity.this);
                fragmentManager.beginTransaction().add(mMenuDialogFragment, "").commit();
            }
        });
//
//        //获取地理位置
//        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

//            updateLocation(location);

        //System.out.println(location.getLatitude() + "hdahsdhasdhkasjd");

//        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() {
//            @Override
//            public void onLocationChanged(Location location) {
//                longitude.delete(0, 1);
//                latitude.delete(0, 1);
//
//                updateLocation(location);
//            }
//
//            @Override
//            public void onStatusChanged(String provider, int status, Bundle extras) {
//
//            }
//
//            @Override
//            public void onProviderEnabled(String provider) {
//
//            }
//
//            @Override
//            public void onProviderDisabled(String provider) {
//
//            }
//        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

//    public void pointChanged(String lat, String lng) {
//
//        latitude.delete(0, 1);
//        longitude.delete(0, 1);
//        latitude.append((Double.valueOf(lat) - 0.00555199));
//        longitude.append((Double.valueOf(lng) - 0.006836000));
//    }

//    public void updateLocation(Location location) {
//
//        longitude.append(location.getLongitude());
//        latitude.append(location.getLatitude());
//
//        pointChanged(latitude.toString(), longitude.toString());
//
//        final ProgressDialog pd = ProgressDialog.show(ShowNearUserMotionActivity.this, getResources().getString(R.string.show_near_user_motion_connecting), getResources().getString(R.string.show_near_user_motion_connecting_to_server));
//
//        lvShowNearUserMotion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                RequestParams params = new RequestParams();
//                params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_NEAR_USER_MOTION);
//                params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
//                params.addBodyParameter(Config.KEY_LATITUDE, latitude.toString());
//                params.addBodyParameter(Config.KEY_LONGTITUDE, longitude.toString());
//                httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
//                    @Override
//                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        pd.dismiss();
//                        Gson gson = new Gson();
//                        list = gson.fromJson(responseInfo.result, new TypeToken<List<ShowNearUserMotion>>() {
//                        }.getType());
//                        for (int i = 0; i < list.size(); i++) {
//                            adapter.add(new NearUserMotionAdapter(list.get(i).getOther_account_num(), list.get(i).getNickname(), list.get(i).getMotion(), list.get(i).getDate()));
//                        }
//
//                        lvShowNearUserMotion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                                String motion_id = list.get(position).getMotion_id();
//                                Intent intent = new Intent(ShowNearUserMotionActivity.this, ShowMotionActivity.class);
//                                intent.putExtra(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
//                                intent.putExtra(Config.KEY_MOTION_ID, motion_id);
//                                startActivity(intent);
//
//                            }
//                        });
//                    }
//
//                    @Override
//                    public void onFailure(HttpException e, String s) {
//                        pd.dismiss();
//                        Toast.makeText(ShowNearUserMotionActivity.this, R.string.show_near_user_motion_fail, Toast.LENGTH_SHORT).show();
//
//                    }
//                });
//
//            }
//        });
//    }

    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.drawable.btn_close);

        MenuObject pals_motion = new MenuObject("朋友圈");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_pals_motion);
        pals_motion.setBitmap(b);

        MenuObject my_motion = new MenuObject("自己动态");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_my_motion);
        my_motion.setBitmap(b);

        MenuObject male_toplist = new MenuObject("男生榜");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.drawable.btn_male_toplist));
        male_toplist.setDrawable(bd);

        MenuObject blacklist = new MenuObject("黑名单");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_blacklist);
        blacklist.setBitmap(b);

        MenuObject female_toplist = new MenuObject("女神榜");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_female_toplist);
        female_toplist.setBitmap(b);

        menuObjects.add(close);
        menuObjects.add(pals_motion);
        menuObjects.add(my_motion);
        menuObjects.add(female_toplist);
        menuObjects.add(male_toplist);
        menuObjects.add(blacklist);

        return menuObjects;
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_image:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
            mMenuDialogFragment.dismiss();
        } else {
            finish();
        }
    }

    public void onMenuItemClick(View clickedView, int position) {
        if (position == 1) {
            Intent intent = new Intent(ShowNearUserMotionActivity.this, MainActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 2) {
            Intent intent = new Intent(ShowNearUserMotionActivity.this, ShowMyMotionActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 3) {
            Intent intent = new Intent(ShowNearUserMotionActivity.this, ShowFemaleToplistActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 4) {
            Intent intent = new Intent(ShowNearUserMotionActivity.this, ShowMaleToplistActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 5) {
            Intent intent = new Intent(ShowNearUserMotionActivity.this, ShowBlacklistActivity.class);
            startActivity(intent);
            finish();

        }
    }

    public void onMenuItemLongClick(View clickedView, int position) {
    }
}
